﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RadowCore.Models
{
    /// <summary>
    /// A node in the exception tree which represents a flow between two originators
    /// </summary>
    class FlowNode:OpNode
    {
        private bool m_isRoleType;
        /// <summary>
        /// a boolean value, determines if true, then: the flow should be between roles, 
        /// otherwise between instances of roles.
        /// </summary>
        public bool IsRoleType
        {
            get { return m_isRoleType; }
            set { m_isRoleType = value; }
        }
        private string m_source;
        /// <summary>
        /// The source originator of the flow
        /// </summary>
        public string Source
        {
            get { return m_source; }
            set { m_source = value; }
        }


        private string m_destination;
        /// <summary>
        /// the destination originator of the flow
        /// </summary>
        public string Destination
        {
            get { return m_destination; }
            set { m_destination = value; }
        }

        /// <summary>
        /// FlowNode class constructor
        /// </summary>
        /// <param name="source">The source originator of the flow</param>
        /// <param name="destination">The destination originator of the flow</param>
        public FlowNode(string source, string destination)
        {
            this.m_source = source;
            this.m_destination = destination;
            this.OpName = "Flow";
        }
        /// <summary>
        /// checks whether a flow from the node's source to the node's destination occured in the log.
        /// </summary>
        /// <returns>true if a flow occured, false otherwise</returns>
        public override bool Evaluate(List<PGNode> visitedNodes)
        {
            List<PGNode> temp = new List<PGNode>();
            if (this.IsRoleType)
            {
                int originatorIndex = visitedNodes.FindIndex(e => e.Role == this.Source);
                for(int i = originatorIndex+1; i < visitedNodes.Count; i++)
                    temp.Add(visitedNodes[i]);
                int destinationIndex = temp.FindIndex(e => e.Role == this.Destination) + originatorIndex + 1;
                if (originatorIndex < 0 || destinationIndex < 0)
                    return false;
                if (originatorIndex < destinationIndex)
                    return true;
                return false;
            }
            else
            {
                int originatorIndex = visitedNodes.FindIndex(e => e.Orignator == this.Source);
                int destinationIndex = visitedNodes.FindIndex(e => e.Orignator == this.Destination);
                if (originatorIndex < 0 || destinationIndex < 0)
                    return false;
                if (originatorIndex <= destinationIndex)
                    return true;
                return false;
            }
        }
    }
}
